#include <iostream>
using namespace std;

int n,c,an;
int main()
{
    int i, j, k, tot;
    while (1)
    {
        cin >> c >> n;
        if (n==0 && c==0) break;
        if (n%2==1)
        {
            an = n;
            j = (n+1)/2;
            for (i=1; i<=j; ++i) an*=c;
        }   
        else 
        {   
            an = (1+c)*n/2;
            j = n/2;
            for (i=1; i<=j; ++i) an*=c;
        }     
        for (k=1,tot=1; k<=n; ++k) tot*=c;
        an += tot;
        for (i=1; i<n; ++i)
        {
            for (j=i; j>=1; j--)
                if (i%j==0 && n%j==0)
                   break;
            for (k=1, tot=1; k<=j; ++k) tot*=c;
            an += tot;
        }
        an /= 2*n;
        cout << an << endl;
       	
    }
        
}    